//
//  TTDianpuSetHeaderView.m
//  TaoTuo
//
//  Created by admin on 2019/11/6.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuSetHeaderView.h"
#import <UIImageView+WebCache.h>
#import "LLImagePicker.h"
#import "OSSImageUploader.h"

@interface DianpuSetheaderSubView : UIView<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *infoString;
@property (nonatomic, copy) NSString *imageUrlString;

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *deleteBtn;

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, copy) void(^uploadSuccessBlock)(NSString *urlString);

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title infoString:(NSString *)infoString imageUrlString:(NSString *)imageUrlString uploadSuccessBlock:(void(^)(NSString *urlString))uploadSuccessBlock;

@end

@implementation DianpuSetheaderSubView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title infoString:(NSString *)infoString imageUrlString:(NSString *)imageUrlString uploadSuccessBlock:(void (^)(NSString *))uploadSuccessBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.uploadSuccessBlock = uploadSuccessBlock;
        self.title = title;
        self.infoString = infoString;
        self.imageUrlString = imageUrlString;
        
        
        UILabel *titleLabel = [UILabel labelWithTitle:title color:UIColorHex(333333) fontSize:13];
        [self addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.centerX.equalTo(self);
        }];
        
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.layer.cornerRadius = 5;
        _imageView.layer.masksToBounds = YES;
        _imageView.userInteractionEnabled = YES;
        [_imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrlString] placeholderImage:[UIImage imageNamed:@"addimage"] options:SDWebImageRetryFailed];
        [self addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).mas_offset(10);
            make.centerX.equalTo(self);
            make.width.height.mas_equalTo(75);
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView:)];
        [_imageView addGestureRecognizer:tap];
        
        _deleteBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"reddelete"] target:self action:@selector(deleteImageAction)];
        [self addSubview:_deleteBtn];
        [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom);
            make.right.equalTo(self.imageView.mas_right).mas_offset(15);
            make.width.height.mas_equalTo(30);
        }];
        
        UILabel *infoLabel = [UILabel labelWithTitle:infoString color:UIColorHex(999999) fontSize:9];
        [self addSubview:infoLabel];
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageView.mas_bottom).mas_offset(3);
            make.centerX.equalTo(self);
        }];
        
        _deleteBtn.hidden = imageUrlString.length == 0;
        
        
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.imageView addSubview:_activityIndicatorView];
        [_activityIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.imageView);
        }];
        
        
    }
    return self;
}

// 点击了图片
- (void)clickImageView:(UITapGestureRecognizer *)tap
{
    // 选取图片
    @weakify(self)
    [LLImagePicker imagePickerWithMaxImagesCount:1 delegate:self baseViewController:[LLRouteTool getCurrentNavigationController] didFinishPickingPhotosHandle:^(NSArray<UIImage *> * _Nonnull photos, BOOL isSelectOriginalPhoto) {
        @strongify(self)
        UIImage *image = photos.firstObject;
        //上传图片到OSS
        [self uploadImageToOSS:image];
        
    }];
    
}

// 拍照或选取图片完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    // 图片
    UIImage *image = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    [self uploadImageToOSS:image];
    
}

///上传图片到OSS
- (void)uploadImageToOSS:(UIImage *)image
{
    [self showActivityView];
    @weakify(self)
    [[OSSImageUploader sharedLoader] asyncUploadImage:image complete:^(NSArray<NSString *> * _Nonnull names, UploadImageState state) {
//        NSLog(@">>>> %@, state = %ld", names, state);
        @strongify(self)
        self.imageView.image = image;
        self.deleteBtn.hidden = NO;
        [self hiddenActivityView];
        if (self.uploadSuccessBlock) {
            self.uploadSuccessBlock(names.firstObject);
        }
    }];
}


// 删除图片
- (void)deleteImageAction
{
    self.imageView.image = [UIImage imageNamed:@"addimage"];
    self.deleteBtn.hidden = YES;
    if (self.uploadSuccessBlock) {
        self.uploadSuccessBlock(@"");
    }
}

- (void)showActivityView
{
    [_activityIndicatorView startAnimating];
}

- (void)hiddenActivityView
{
    [_activityIndicatorView stopAnimating];
}

@end

NSString *const shangchengImageUrlString = @"ShopLogo";
NSString *const erweimaImageUrlString = @"ShopWeChatQRCode";
NSString *const homebgImageUrlString = @"ShopBackgroundImg";

@interface TTDianpuSetHeaderView ()

@property (nonatomic, copy) NSString *logoUrlString;
@property (nonatomic, copy) NSString *erWeiMaUrlString;
@property (nonatomic, copy) NSString *bgUrlString;
@property (nonatomic, copy) void(^upLoadResult)(NSMutableDictionary *dic);
@property (nonatomic, strong) NSMutableDictionary *dic;

@end

@implementation TTDianpuSetHeaderView

+ (instancetype)headerViewWithLogo:(NSString *)logoUrlString erWeiMa:(NSString *)erWeiMaUrlString bgUrlString:(NSString *)bgUrlString upLoadResult:(nonnull void (^)(NSMutableDictionary * _Nonnull))upLoadResult
{
    return [[self alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 147) logo:logoUrlString erWeiMa:erWeiMaUrlString bgUrlString:bgUrlString upLoadResult:upLoadResult];
}

- (instancetype)initWithFrame:(CGRect)frame logo:(NSString *)logoUrlString erWeiMa:(NSString *)erWeiMaUrlString bgUrlString:(NSString *)bgUrlString upLoadResult:(nonnull void (^)(NSMutableDictionary * _Nonnull))upLoadResult
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.upLoadResult = upLoadResult;
        self.logoUrlString = logoUrlString;
        self.erWeiMaUrlString = erWeiMaUrlString;
        self.bgUrlString = bgUrlString;
        
        self.dic = [NSMutableDictionary dictionary];
        // 初始值
        [self.dic setObject:logoUrlString forKey:shangchengImageUrlString];
        [self.dic setObject:erWeiMaUrlString forKey:erweimaImageUrlString];
        [self.dic setObject:bgUrlString forKey:homebgImageUrlString];
        
        [self setupSubviews];
        
        if (self.upLoadResult) {
            self.upLoadResult(self.dic);
        }
        
        
    }
    return self;
}

- (void)setupSubviews
{
    @weakify(self)
    DianpuSetheaderSubView *logoView = [[DianpuSetheaderSubView alloc] initWithFrame:CGRectMake(2, 15, 91, 122) title:@"商城LOGO" infoString:@"建议尺寸:100*100" imageUrlString:self.logoUrlString uploadSuccessBlock:^(NSString *urlString) {
        @strongify(self)
        [self.dic setObject:urlString forKey:shangchengImageUrlString];
        if (self.upLoadResult) {
            self.upLoadResult(self.dic);
        }
        
    }];
    [self addSubview:logoView];
    
    
    DianpuSetheaderSubView *erweimaView = [[DianpuSetheaderSubView alloc] initWithFrame:CGRectMake(logoView.right, logoView.top, logoView.width, logoView.height) title:@"微信二维码" infoString:@"建议尺寸:300*300" imageUrlString:self.erWeiMaUrlString uploadSuccessBlock:^(NSString *urlString) {
        @strongify(self)
        [self.dic setObject:urlString forKey:erweimaImageUrlString];
        if (self.upLoadResult) {
            self.upLoadResult(self.dic);
        }
    }];
    [self addSubview:erweimaView];
    
    DianpuSetheaderSubView *bgImageView = [[DianpuSetheaderSubView alloc] initWithFrame:CGRectMake(erweimaView.right, logoView.top, logoView.width, logoView.height) title:@"首页背景图" infoString:@"建议尺寸:750*360" imageUrlString:self.bgUrlString uploadSuccessBlock:^(NSString *urlString) {
        @strongify(self)
        [self.dic setObject:urlString forKey:homebgImageUrlString];
        if (self.upLoadResult) {
            self.upLoadResult(self.dic);
        }
    }];
    [self addSubview:bgImageView];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(0, 137) endPoint:CGPointMake(self.width, 137) lineColor:UIColorHex(F1F5F6) lineWidth:10];
}


@end
